package com.yc.cloud.openai.service.impl;

import cn.hutool.core.util.StrUtil;
import com.yc.cloud.openai.dto.NlpChunkResultDto;
import com.yc.cloud.openai.dto.OpenAiResultDto;
import com.yc.cloud.openai.dto.TTSResultDTO;
import com.yc.cloud.openai.enums.SupplierEnums;
import com.yc.cloud.openai.request.NLPParam;
import com.yc.cloud.openai.request.OpenAiParam;
import com.yc.cloud.openai.request.TTSParam;
import com.yc.cloud.openai.service.NLPService;
import com.yc.cloud.openai.service.OpenAiService;
import com.yc.cloud.openai.service.tts.TTSService;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import reactor.core.publisher.Flux;

import java.util.Map;

@Slf4j
@Service("XFOpenAiServiceImpl")
public class XFOpenAiServiceImpl implements OpenAiService {
    private final NLPService nlpService;
    private final TTSService ttsService;


    public XFOpenAiServiceImpl(@Qualifier("XFNLPApiServiceImpl") NLPService nlpService, @Qualifier("XFTTSServiceImpl") TTSService ttsService) {
        this.nlpService = nlpService;
        this.ttsService = ttsService;
    }

    @Override
    public String getIdentifier() {
        return SupplierEnums.TCZLLD.getCode();
    }

    @Override
    public OpenAiResultDto analysis(OpenAiParam openAiParam) {
        val nlpParam = new NLPParam();
        BeanUtils.copyProperties(openAiParam, nlpParam);
        nlpParam.setContent(openAiParam.getPath());
        val nlpResult = nlpService.getNLPResult(nlpParam);
        if (nlpResult == null) {
            return null;
        }
        val result = new OpenAiResultDto();
        //科大讯飞tts,nlp,iat同时处理的
        result.setNlpCostTime(nlpResult.getCostTime());
        result.setIatContent(nlpResult.getIatResult());
        result.setTtsContent(nlpResult.getTtsResult());
        result.setNlpContent(nlpResult.getResult());
        return result;
    }

    @Override
    public TTSResultDTO getTTSResult(OpenAiParam openAiParam) {
        val ttsParam = new TTSParam();
        ttsParam.setFormat(openAiParam.getFormat());
        ttsParam.setContent(openAiParam.getContent());
        ttsParam.setGender(openAiParam.getGender());
        //如果其他厂商算法单独TTS,需要设置ttsKey和ttsAppid
        if (StrUtil.isNotEmpty(openAiParam.getTtsAppid())) {
            ttsParam.setAppid(openAiParam.getTtsAppid());
        }
        if (StrUtil.isNotEmpty(openAiParam.getTtsAppKey())) {
            ttsParam.setAppKey(openAiParam.getTtsAppKey());
        }
        return ttsService.getTTSResult(ttsParam);
    }

    @Override
    public Flux<String> analysisByStream(OpenAiParam openAiParam) {
        return null;
    }

    @Override
    public Flux<ServerSentEvent<String>> analysisByStream2(OpenAiParam param) {
        return null;
    }

    @Override
    public SseEmitter analysisByStream3(OpenAiParam param) {
        return null;
    }
}
